一樣的模式再來一次,根據以上的Reqres API 來示範
首先一樣根據Response 建立Model
User.swift
:
struct UserUpdateResponse: Decodable {
var name: String
var job: String
var updatedAt: Date?
var modifiedUpdateAt: String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
if let newDate = updatedAt {
return dateFormatter.string(from: newDate)
} else {
return "******"
}
}
enum CodingKeys: String, CodingKey {
case name
case job
case updatedAt
}
}
接著在UserURLSession.swift
處理關於URLSession
的事件
func userUpdateRequest(userRequestBody:UserRequestBody,completionHandler: @escaping (UserUpdateResponse) -> Void) {
let url = URL(string: "https://reqres.in/api/users/2")!
// MARK: URLRequest
var request = URLRequest(url: url)
request.httpMethod = "PUT"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
//MARK: POST Request V.1.0
guard let httpBody = try? JSONEncoder().encode(userRequestBody) else {
print("Invalid httpBody")
return
}
// MARK: Set httpBody
request.httpBody = httpBody
URLSession.shared.dataTask(with: request) {
data, response, error in
if let data = data {
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(DateFormatter.customFormatter)
let user = try decoder.decode(UserUpdateResponse.self, from: data)
completionHandler(user)
}catch(let error) {
print(error.localizedDescription)
}
} else {
print("No Data")
}
}.resume()
}
}
之後在View 設計畫面並使用UPDATESampleView.swift
:
import SwiftUI
struct UPDATESampleView: View {
@State private var name = "Ryder"
@State private var job = "Engineer"
@State var user :UserUpdateResponse = UserUpdateResponse(name: "*****", job: "*****", updatedAt: nil)
var body: some View {
NavigationView {
VStack {
List(){
Text("UPDATE sample")
Section(header: Text("Request Data")) {
TextField("Name: ", text: $name)
TextField("Job: ", text: $job)
}
}
List() {
Section(header: Text("Response Data")) {
Text("Name: \(user.name)")
Text("Job: \(user.job)")
Text("Date updated: \(user.modifiedUpdateAt)")
}
}
.listStyle(GroupedListStyle())
Button {
UserURLSession.shared.userUpdateRequest(userRequestBody: UserRequestBody(name: name, job: job)) { newUser in
DispatchQueue.main.async {
user = newUser
}
}
} label: {
Text("UPDATE Request")
}
.padding()
.foregroundColor(Color.white)
.background(Color.green)
.cornerRadius(8)
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(Text("URLSession UPDATE Sample"))
}
}
}
struct UPDATESampleView_Previews: PreviewProvider {
static var previews: some View {
UPDATESampleView()
}
}
這邊所有URLSession 的範例一起放在Github: URLSessionSample 供大家參考